package com.company;

import com.company.cilin.Cilin;
import com.company.cilin.CilinDb;

import java.io.IOException;
import java.util.Set;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016.8.15.
 */
public class Procedure {
    private CilinDb cilin;

    private Trie trie;

    public Procedure() throws IOException {
        cilin=new CilinDb();
        trie=new Trie("src/dictionary.txt");
    }

    //看两个词的词义是否相近（在同一原子群)
    public boolean isSynonymy(String word1,String word2){
        Set<String> stringSet1=cilin.getCilinCoding(word1);
        Set<String> stringSet2=cilin.getCilinCoding(word2);

        for (String str1:stringSet1) {
            for (String str2:stringSet2) {
                if (str1.equals(str2)
                        && (str1.charAt(7) != '#')) {
                    return true;
                }
            }
        }

        if(word1.equals(word2)){
            return true;
        }

        return false;
    }

    //给出一个词，输出其所有同义词
    public ArrayList getSynonyms(String word){
        Set<String> ID=cilin.getCilinCoding(word);

        ArrayList synonymsID=new ArrayList();

        ArrayList synonymsWords=new ArrayList();

        for (Object aID : ID) {
            if (((String) aID).charAt(7) != '#') {
                synonymsID.add(aID);
            }
        }

        for (Object aSynonymsID : synonymsID) {
            Set<String> pileWords = cilin.getCilinWords((String) aSynonymsID);

            for (String pileWord : pileWords) {

                int z;
                z = 0;
                for (int k = 0; k < synonymsWords.size(); k++) {
                    if (pileWord.equals(synonymsWords.get(k))) {
                        z = 1;
                        break;
                    }
                }

                if (z == 0) {
                    synonymsWords.add(pileWord);
                }

            }
        }

        return synonymsWords;
    }

    //用我们的算法计算两个词之间的最大距离
    public double maxDistance(String word1,String word2){
        return Calculate.distance(word1,word2,trie,cilin);
    }
}
